/**
 * 程序主入口
 * Main函数
 * */

package com.example;

import com.example.servlets.DataServlet;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import com.example.servlets.AccountServlet;

public class Main {
    public static void main(String[] args) throws Exception {
        // 设置HTTP服务端口号
        Server server = new Server(8080);

        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirectoriesListed(false);
        // 设置静态资源的基本路径
        resourceHandler.setResourceBase("web/");

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);

        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new org.eclipse.jetty.server.Handler[] { resourceHandler, context });

        context.setContextPath("/");
        server.setHandler(handlers);

        // 注册Servlet
        context.addServlet(new ServletHolder(new AccountServlet()), "/api/account");
        context.addServlet(new ServletHolder(new DataServlet()), "/api/data");

        try {
            server.start();
            server.join();
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            server.destroy();
        }
    }
}
